package com.ostms.adv.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

public class MonitorWeixinPayService extends Service {
    public static final String TAG = MonitorWeixinPayService.class.getSimpleName();

    private static final int isStayWeixinMainActivityMsg = 102;

    private static int enterWeixinCount = 0;

    private Handler mHandler = new NoLeakHandler(this);

    Timer timer = new Timer();

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            mHandler.sendEmptyMessage(isStayWeixinMainActivityMsg);
        }
    };

    private void setTimer() {
        timer.schedule(task, 0, 5000); // timeTask
    }

    //创建服务时调用
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
    }

    //服务执行的操作
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");
        enterWeixinCount = 1;
        setTimer();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopTimer();
        Log.d(TAG, "onDestroy");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private static boolean isStayWeixinPayMainActivity() {
        BufferedReader reader = null;
        String content = "";
        String line;
        try {
            Process process = Runtime.getRuntime().exec("dumpsys activity");
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuffer output = new StringBuffer();

            while ((line = reader.readLine()) != null) {
                if (line.indexOf("mFoc") != -1) {
                    output.append(line);
                }
            }
            reader.close();
            content = output.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (content.contains("LposActivity")) {
            Log.i(TAG, "isStayWeixinPayMainActivity：true!");
            enterWeixinCount++;
            Log.i(TAG,"enterWeixinCount = "+enterWeixinCount);
            if(enterWeixinCount>2) {
                enterWeixinCount = 0;
                return true;
            }else{
                return false;
            }
        }
        Log.i(TAG, "isStayWeixinPayMainActivity：false!");
        return false;
    }

    private void stopTimer() {
        task.cancel();
    }

    private static class NoLeakHandler extends Handler {

        private WeakReference<MonitorWeixinPayService> weakReference;

        public NoLeakHandler(MonitorWeixinPayService service){
            weakReference = new WeakReference<>(service);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case isStayWeixinMainActivityMsg:
                    //判断是否用户停留在微信支付页面
                    Log.i(TAG, "handleMessage");
                    if (isStayWeixinPayMainActivity()) {
                        try {
                            Runtime.getRuntime().exec("am start -n com.ostms.adv/com.ostms.adv" +
                                    ".activity.FaceDetectActivity");

                        } catch (Exception ex) {
                            Log.d("exe adb command 抛异常了", ex.getMessage());
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
